package com.donkingliang.labelsviewdemo;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.donkingliang.labels.LabelsBean;
import com.donkingliang.labels.LabelsView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static com.donkingliang.labelsviewdemo.R.id.labels;

public class MainActivity extends AppCompatActivity {

    private LabelsView mButton;
    private LabelsView mLabelsView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        mButton = findViewById(R.id.btnLabels);
        mLabelsView = findViewById(labels);
        // 按钮组
        ArrayList<String> btns = new ArrayList<>();
        btns.add("0不可选中");
        btns.add("1单选(可反选)");
        btns.add("2单选(不可反选)");
        btns.add("3多选");
        btns.add("4多选(最多5个)");
        btns.add("5多选(最少1个)");
        btns.add("6多选(1,2必选)");
        btns.add("7指示器模式");
        btns.add("8取消选中");
        btns.add("9点击");
        btns.add("10在列表中使用");
        mButton.setLabels(btns);
        mButton.setOnLabelClickListener(new LabelsView.OnLabelClickListener() {
            @Override
            public void onLabelClick(TextView label, Object data, int position) {
                if (position != 10) {
                    mLabelsView.setOnLabelClickListener(null);
                    mLabelsView.clearCompulsorys();
                }
                switch (position) {
                    case 0:// 不可选中
                        mLabelsView.setSelectType(LabelsView.SelectType.NONE);
                        break;
                    case 1:// 单选(可反选)
                        mLabelsView.setSelectType(LabelsView.SelectType.SINGLE);
                        break;
                    case 2:// 单选(不可反选)
                        mLabelsView.setSelectType(LabelsView.SelectType.SINGLE_IRREVOCABLY);
                        break;
                    case 3:// 多选
                        mLabelsView.setSelectType(LabelsView.SelectType.MULTI);
                        mLabelsView.setMaxSelect(0);
                        mLabelsView.setMinSelect(0);
                        break;
                    case 4:// 多选(最多5个)
                        mLabelsView.setSelectType(LabelsView.SelectType.MULTI);
                        mLabelsView.setMaxSelect(5);
                        mLabelsView.setMinSelect(0);
                        break;
                    case 5:// 多选(最少1个)
                        mLabelsView.setSelectType(LabelsView.SelectType.MULTI);
                        mLabelsView.setMaxSelect(0);
                        mLabelsView.setMinSelect(1);
                        break;
                    case 6:// 多选(1,2必选)
                        mLabelsView.setSelectType(LabelsView.SelectType.MULTI);
                        mLabelsView.setMaxSelect(0);
                        mLabelsView.setMinSelect(0);
                        mLabelsView.setCompulsorys(0, 1);
                        break;
                    case 7:// 指示器模式
                        mLabelsView.setIndicator(!mLabelsView.isIndicator());
                        if (mLabelsView.isIndicator()) {
                            mButton.getLabels().set(position, "取消指示器模式");
                            label.setText("7取消指示器模式");
                        } else {
                            mButton.getLabels().set(position, "指示器模式");
                            label.setText("7指示器模式");
                        }
                        break;
                    case 8:// 取消选中
                        mLabelsView.clearAllSelect();
                        break;
                    case 9:// 点击
                        mLabelsView.setSelectType(LabelsView.SelectType.NONE);
                        mLabelsView.setOnLabelClickListener(new LabelsView.OnLabelClickListener() {
                            @Override
                            public void onLabelClick(TextView label, Object data, int position) {
                                // Toast.makeText(MainActivity.this, position + " : " + data, Toast.LENGTH_LONG).show();
                                List<Object> selectLabelDatas = mLabelsView.getSelectLabelDatas();
                                List<Integer> selectLabels = mLabelsView.getSelectLabels();

                            }
                        });
                        break;
                    case 10:// 在列表中显示
                        startActivity(new Intent(MainActivity.this, RecyclerViewActivity.class));
                        break;
                }
            }
        });

        //测试的数据
//        ArrayList<String> label = new ArrayList<>();
//        label.add("Android");
//        label.add("IOS");
//        label.add("前端");
//        label.add("后台");
//        label.add("微信开发");
//        label.add("游戏开发");
//        label.add("Java");
//        label.add("JavaScript");
//        label.add("C++");
//        label.add("PHP");
//        label.add("Python");
//        label.add("Swift");
//        labelsView.setLabels(label);

        ArrayList<LabelsBean> testList = new ArrayList<>();
        testList.add(new LabelsBean("Android", 1));
        testList.add(new LabelsBean("IOS", 2));
        testList.add(new LabelsBean("前端", 3));
        testList.add(new LabelsBean("后台", 4));
        testList.add(new LabelsBean("微信开发", 5));
        testList.add(new LabelsBean("游戏开发", 6));
        testList.add(new LabelsBean("Java", 7));
        testList.add(new LabelsBean("JavaScript", 8));
        testList.add(new LabelsBean("C++", 9));
        testList.add(new LabelsBean("PHP", 10));
        testList.add(new LabelsBean("Python", 11));
        testList.add(new LabelsBean("Swift", 12));

        mLabelsView.setLabels(testList, new LabelsView.LabelTextProvider<LabelsBean>() {
            @Override
            public CharSequence getLabelText(TextView label, int position, LabelsBean data) {
                return data.getName();
            }
        });

        // 设置最大显示行数，小于等于0则不限行数。
//        labelsView.setMaxLines(1);

        mLabelsView.clearAllSelect();
    }
}
